package com.itheima.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.FileNotFoundException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandle {

    @ExceptionHandler( SQLIntegrityConstraintViolationException.class)
    public R<String> getException(Exception ex){
        ex.printStackTrace();
        String[] msg = ex.getMessage().split(" ");
        if (ex.getMessage().contains("Duplicate entry")){
            return R.error(msg[9]+"已存在");
        }
        return R.error("错误");
    }

    @ExceptionHandler(RelationException.class)
    public R<String> getRelationException(Exception ex){
        return R.error(ex.getMessage());
    }

    @ExceptionHandler(FileNotFoundException.class)
    public void getFileNotFoundException(Exception ex){
        log.info("暂不抛异常");
    }
}
